package leetcode.weekly.week290;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution3xy implements IDebug {

	public int[] countRectangles(int[][] rectangles, int[][] points) {
		int len = rectangles.length;
		Set<Integer> set = new HashSet<>();
		for (int i = 0; i < len; i++) {
			set.add(rectangles[i][1]);
		}
		int size = set.size();
		List<Integer> list = new ArrayList<>(set);
		Collections.sort(list);
		Map<Integer, Integer> hash = new HashMap<>();
		int[] yy = new int[size];
		for (int i = 0; i < list.size(); i++) {
			int y = list.get(i);
			hash.put(y, i);
			yy[i] = y;
		}
		ArrayList<Integer>[] arr = new ArrayList[size];
		for (int i = 0; i < arr.length; i++) {
			arr[i] = new ArrayList<>();

		}
		for (int i = 0; i < len; i++) {
			int[] rectangle = rectangles[i];
			int l = rectangle[0], h = rectangle[1];
			int idx = hash.get(h);
			arr[idx].add(l);
		}
		for (int i = 0; i < arr.length; i++) {
			Collections.sort(arr[i]);
		}
		int[] ans = new int[points.length];
		for (int i = 0; i < points.length; i++) {
			int[] point = points[i];
			int x = point[0], y = point[1];
			int idy = this.find(yy, y);
			ans[i] = idy == -1 ? 0 : this.find(arr, idy, x);
		}
		return ans;
	}

	private int find(ArrayList<Integer>[] arr, int idy, int x) {
		int ans = 0;
		for (int i = idy; i < arr.length; i++) {
			List<Integer> list = arr[i];
			ans += this.findList(list, x);
		}
		return ans;
	}

	// array上找到大于value的最小索引
	private int findList(List<Integer> array, int value) {
		int l = 0, r = array.size() - 1;
		int ans = -1;
		while (l <= r) {
			int mid = l + ((r - l) >> 1);
			if (array.get(mid) >= value) {
				ans = mid;
				r = mid - 1;
			} else {
				l = mid + 1;
			}
		}
		return ans == -1 ? 0 : array.size() - ans;
	}

	// array上找到大于value的最小索引
	private int find(int[] array, int value) {
		int l = 0, r = array.length - 1;
		int ans = -1;
		while (l <= r) {
			int mid = l + ((r - l) >> 1);
			if (array[mid] >= value) {
				ans = mid;
				r = mid - 1;
			} else {
				l = mid + 1;
			}
		}
		return ans;
	}

	@Override
	public void debug4() {
		int[] ans = H.str2array3("[2,1]");
		H.compare(ans, this.countRectangles(H.str2array1("[[1,2],[2,3],[2,5]]"), H.str2array1("[[2,1],[1,4]]")));

	}

	@Override
	public void debug3() {
		int[] ans = H.str2array3("[1,3]");
		H.compare(ans, this.countRectangles(H.str2array1("[[1,1],[2,2],[3,3]]"), H.str2array1("[[1,3],[1,1]]")));
	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub
		int[] ans = H.str2array3("[5,0]");
		H.compare(ans,
				this.countRectangles(H.str2array1("[[4,7],[4,9],[8,5],[6,2],[6,4]]"), H.str2array1("[[4,2],[5,6]]")));
	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub
		int[] ans = H.str2array3("[1,0,4,1,0,0,0,1]");
		H.compare(ans, this.countRectangles(H.str2array1("[[7,1],[2,6],[1,4],[5,2],[10,3],[2,4],[5,9]]"),
				H.str2array1("[[10,3],[8,10],[2,3],[5,4],[8,5],[7,10],[6,6],[3,6]]")));

//		[[7,1],[2,6],[1,4],[5,2],[10,3],[2,4],[5,9]]
//		[[10,3],[8,10],[2,3],[5,4],[8,5],[7,10],[6,6],[3,6]]
	}

	public static void main(String[] args) {
		Solution3xy so = new Solution3xy();
		so.debug1();
//		so.debug2();
//		so.debug3();
//		so.debug4();

	}

}
